package org.platform.processor.knowledge;

import com.mc.mic.core.exception.IllegalParamException;
import org.platform.dao.mapper.FdDiagnoseTaskTreeNodeMapper;
import org.platform.dao.mapper.FdKbKnowledgemainMapper;
import org.platform.dao.mapper.FdKbTreemainMapper;
import org.platform.dao.mapper.FdKbTreenodeMapper;
import org.platform.mo.csmo.common.*;
import org.platform.mo.db.FdDiagnoseTaskTreeNode;
import org.platform.mo.db.FdKbKnowledgemain;
import org.platform.mo.db.FdKbTreemain;
import org.platform.mo.db.FdKbTreenode;
import org.platform.processor.diagnose.mo.DiagnoseTreeBean;
import org.platform.processor.diagnose.mo.DiagnoseTreeNodeBean;
import org.platform.tool.util.BeansUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;

import java.util.ArrayList;
import java.util.List;

/**
 * 故障树后台执行模块
 *
 * @author FengJie
 * @date 2018/4/20
 */
@Service
public class TreeProcessor {
    @Autowired
    private FdKbKnowledgemainMapper knowledgeMainMapper;
    @Autowired
    private FdKbTreenodeMapper treeNodeMapper;
    @Autowired
    private FdKbTreemainMapper treeMainMapper;
    @Autowired
    private FdDiagnoseTaskTreeNodeMapper treeNodeDiagnoseMapper;

    /**
     * 根据知识,查询对应的故障树/故障树节点
     *
     * @param main
     * @return
     */
    public KnowTreeApplyBean getKnowTreeByKBMain(KB_Main main) {
        KnowTreeApplyBean applyBean = new KnowTreeApplyBean();

        Example mainExample = new Example(FdKbTreemain.class);
        Example.Criteria criteria = mainExample.createCriteria()
                .andEqualTo("knowledgeid", main.getKnowledgeid())
                .andEqualTo("version", main.getVersion());
        List<FdKbTreemain> treeMainList = treeMainMapper.selectByExample(mainExample);
        KB_Tree tree = new KB_Tree();
        if (1 == treeMainList.size()) {
            BeanUtils.copyProperties(treeMainList.get(0), tree);
        }
        applyBean.setTreeMain(tree);

        Example nodeExample = new Example(FdKbTreenode.class);
        nodeExample.and(criteria);
        List<FdKbTreenode> treeNodeList = treeNodeMapper.selectByExample(nodeExample);
        List<KB_TreeNode> nodeList = BeansUtils.copyListProperties(treeNodeList, KB_TreeNode.class);
        applyBean.setTreeNodeList(nodeList);
        return applyBean;
    }

    /**
     * 插入对应的故障树和故障树节点
     *
     * @param applyBean
     */
    public void addKnowTree(KnowTreeApplyBean applyBean) {
        KB_Tree treeMain = applyBean.getTreeMain();
        FdKbTreemain main = new FdKbTreemain();
        BeanUtils.copyProperties(treeMain, main);
        treeMainMapper.insertSelective(main);

        List<KB_TreeNode> treeNodeList = applyBean.getTreeNodeList();
        List<FdKbTreenode> nodeList = BeansUtils.copyListProperties(treeNodeList, FdKbTreenode.class);
        nodeList.forEach(node -> treeNodeMapper.insert(node));
    }

    /**
     * 根据知识id 获取故障诊断bean类
     *
     * @param knowledgeID
     * @return
     */
    public DiagnoseTreeBean getDiagTreeBeanByKnowledgeID(Long knowledgeID) {
        Example example = new Example(FdKbTreemain.class);
        Example.Criteria criteria = example.createCriteria().
                andEqualTo("knowledgeid", knowledgeID);
        List<FdKbTreemain> treeMainList = treeMainMapper.selectByExample(knowledgeID);
        List<DiagnoseTreeBean> diagnoseTreeBeanList = BeansUtils.copyListProperties(treeMainList, DiagnoseTreeBean.class);
        DiagnoseTreeBean diagnoseTreeBean = null;
        if (diagnoseTreeBeanList.size() == 1) {
            diagnoseTreeBean = diagnoseTreeBeanList.get(0);
        } else {
            throw new IllegalParamException("故障树诊断参数异常");
        }

        Example nodeExample = new Example(FdKbTreemain.class);
        nodeExample.and(criteria);

        List<FdKbTreenode> treeNodeList = treeNodeMapper.selectByExample(nodeExample);
        List<DiagnoseTreeNodeBean> diagnoseTreeNodeBeanList = BeansUtils.copyListProperties(treeNodeList, DiagnoseTreeNodeBean.class);
        diagnoseTreeBean.setDiagnoseTreeNodeBeanList(diagnoseTreeNodeBeanList);
        return diagnoseTreeBean;
    }

    /**
     * 根据诊断ID查询需要诊断的故障树或诊断结果
     *
     * @param diagnoseId
     * @return
     */
    public List<FD_DiagnoseTaskTreeNode> queryTreeNodeByDiagnoseId(String diagnoseId) {
        Example example = new Example(FdDiagnoseTaskTreeNode.class);
        example.createCriteria().andEqualTo("diagnoseid", diagnoseId);
        List<FdDiagnoseTaskTreeNode> fdTreeNodeList = treeNodeDiagnoseMapper.selectByExample(example);
        return BeansUtils.copyListProperties(fdTreeNodeList, FD_DiagnoseTaskTreeNode.class);
    }

    public List<FD_DiagnoseTaskTreeNode> queryTreeNodeByDiagnoseIdAndType(String diagnoseId) {
        Example example = new Example(FdDiagnoseTaskTreeNode.class);
        example.createCriteria()
                .andEqualTo("diagnoseid", diagnoseId)
                .andNotEqualTo("nodetype", 2);
        List<FdDiagnoseTaskTreeNode> fdTreeNodeList = treeNodeDiagnoseMapper.selectByExample(example);
        return BeansUtils.copyListProperties(fdTreeNodeList, FD_DiagnoseTaskTreeNode.class);
    }

    public List<FdDiagnoseTaskTreeNode> queryFdTreeNodeByDiagnoseId(String diagnoseId) {
        Example example = new Example(FdDiagnoseTaskTreeNode.class);
        example.createCriteria().andEqualTo("diagnoseid", diagnoseId);
        return treeNodeDiagnoseMapper.selectByExample(example);
    }


    /**
     * 删除历史版本
     *
     * @param main
     */
    public void delOldKnowTreeByKBMain(KB_Main main) {
        Example mainExample = new Example(FdKbTreemain.class);
        Example.Criteria criteria = mainExample.createCriteria()
                .andEqualTo("knowledgeid", main.getKnowledgeid())
                .andLessThan("version", main.getVersion());
        treeMainMapper.deleteByExample(mainExample);

        Example nodeExample = new Example(FdKbTreenode.class);
        nodeExample.and(criteria);
        treeNodeMapper.deleteByExample(nodeExample);
    }

    /**
     * 根据知识ID查询故障树
     *
     * @param treeId 知识ID
     * @return 故障树所有节点
     */
    public List<FdKbTreenode> queryTreeNodeByTreeID(Long treeId) {
        Example example = new Example(FdKbTreenode.class);
        example.createCriteria().
                andEqualTo("treeid", treeId);
        return treeNodeMapper.selectByExample(example);
    }

    /**
     * 根据设备类型查询故障树
     *
     * @param deviceType 设备类型
     * @return 故障树节点
     */
    public List<FdKbTreenode> queryTreeNodeByDevID(String deviceType) {
        Example example = new Example(FdKbTreemain.class);
        example.createCriteria().
                andEqualTo("devicetype", deviceType);
        List<FdKbTreemain> treeList = treeMainMapper.selectByExample(example);
        if (treeList.size() > 0) {
            return queryTreeNodeByTreeID(treeList.get(0).getTreeid());
        }
        return new ArrayList<>();
    }


    /**
     * 根据知识ID查询对应的知识类型
     *
     * @param knowledgeId
     * @return
     */
    public Integer getKnowledgeTypeByKnowledgeId(Long knowledgeId) {
        Example example = new Example(FdKbKnowledgemain.class);
        example.createCriteria().andEqualTo("knowledgeid", knowledgeId);
        List<FdKbKnowledgemain> knowledgeList = knowledgeMainMapper.selectByExample(example);
        if (knowledgeList.size() > 0) {
            return knowledgeList.get(0).getKnowledgetype();
        }
        return null;
    }

}
